package com.example.healkep.MainActivity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.example.healkep.R;
import com.example.healkep.bean.globalUserInfo;
import com.example.healkep.datepicker.CustomDatePicker;
import com.example.healkep.datepicker.DateFormatUtils;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;


public class MainActivityGetinFood extends Activity {
    private int time_slot = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate ( savedInstanceState );
        setContentView ( R.layout.activity_getin_food );
        Button back = findViewById ( R.id.back );
        back.setOnClickListener ( new View.OnClickListener () {
            @Override
            public void onClick(View view) {
                finish ();
            }
        } );
        final EditText name = findViewById(R.id.name);
        final EditText cal = findViewById(R.id.cal);
        final TextView date = findViewById(R.id.date);
        String beginTime = "2018-00-00 00:00";
        String endTime = DateFormatUtils.long2Str(System.currentTimeMillis(), true);
        date.setText(endTime);
        date.setOnClickListener((i)->{
            new CustomDatePicker(MainActivityGetinFood.this, new CustomDatePicker.Callback() {
                @Override
                public void onTimeSelected(long timestamp) {
                    date.setText(DateFormatUtils.long2Str(timestamp, true));
                }
            }, beginTime, endTime).show(endTime);
        });
        // 初始化控件
        final Spinner time = (Spinner) findViewById(R.id.time);
// 建立数据源
        String[] mItems = getResources().getStringArray(R.array.food_time);
// 建立Adapter并且绑定数据源
        ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, mItems);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//绑定 Adapter到控件
        time .setAdapter(adapter);
        time.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener () {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int pos, long id) {

                String[] foodtime = getResources().getStringArray(R.array.food_time);
                time_slot = pos + 1;
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // Another interface callback
            }
        });
        Button button1 = findViewById ( R.id.login );
        button1.setOnClickListener ( new View.OnClickListener () {
            @Override
            public void onClick(View view) {
                String id = name.getText().toString();
                String finalcal = cal.getText().toString();
                String finaldate = date.getText().toString ();
                String user = globalUserInfo.getInstance().getUsername();
                int finaltime = time_slot;
                final String json ="{\n" +
                        "    \"user\":\""+user+"\",\n" +
                        "    \"time_slot\":\""+finaltime+"\",\n" +
                        "    \"id\":\""+id+"\",\n" +
                        "    \"cal\":\""+finalcal+"\",\n" +
                        "    \"cdate\":\""+finaldate+"\"\n" +
                        "}";
                new Thread ( new Runnable () {
                    @Override
                    public void run() {
                        try{
                            OkHttpClient client = new OkHttpClient (  );
                            Request request = new Request.Builder (  )
                                    .url ( com.example.healkep.LoginActivity.url+"/food2/add" )
                                    .post ( RequestBody.create ( MediaType.parse ( "application/json"),json  ) )
                                    .build ();
                            Response response = client.newCall ( request ).execute ();
                            runOnUiThread ( new Runnable () {
                                @Override
                                public void run() {
                                    Toast.makeText ( MainActivityGetinFood.this, "发送成功" , Toast.LENGTH_SHORT).show ();

                                }
                            } );
                        }catch (Exception e){
                            e.printStackTrace ();
                            runOnUiThread ( new Runnable () {
                                @Override
                                public void run() {
                                    Toast.makeText ( MainActivityGetinFood.this, "网络连接失败", Toast.LENGTH_SHORT).show ();
                                }
                            } );
                        }
                    }
                } ).start ();
            }
        } );

    }
}
